package com.todoclipse.common;

import org.json.simple.JSONArray;

/**
 * Interface which should be implemented in order to provide access to the todoist.com server
 * Manager uses this class for obtaining todoist data.
 * @author eximius
 *
 */
public interface Request {
	/**
	 * This class holds keys and values for request query
	 * @author eximius
	 *
	 */
	public static class KeyValue{
		private final String key;
		private final String value;
		private final String string;

		/**
		 * Constructor
		 * @param key query key (Can not be null)
		 * @param value query value
		 */
		public KeyValue(String key, String value) {
			if(key == null){
				throw new NullPointerException(getClass().getName()+": Key can not be null");
			}
			this.key = key;
			this.value = value;
			this.string = new StringBuilder().append(key).append("=").append(value).toString();
		}
		public String getKey() {
			return key;
		}
		public String getValue() {
			return value;
		}
		@Override
		public boolean equals(Object obj) {
			boolean result = (this == obj);
			if (result == false && obj instanceof KeyValue) {
				KeyValue kv = (KeyValue) obj;
				result = this.string.equals(kv.string);
			}
			return result;
		}
		@Override
		public int hashCode() {
			return string.hashCode();
		}
		@Override
		public String toString() {
			return string;
		}
	}
	/**
	 * This method shoud take todoist.com API method name (e.g. getAllProjects)
	 * and optionaal query (e.g. id=666). As a result it returns data returned by todoist.com server
	 * If returned object is not an JSONArray, Request implementation has to create one, containing the result.
	 * @param method todoist.com API method
	 * @param queryParams optional query parameters, can be null
	 * @return JSONArray returned by todoist.com server. It can be null if server returns nothing.
	 */
	public JSONArray invoke(String method, KeyValue ... queryParams);
}
